package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServicelmpl implements OrderService {
    @Autowired
    public ShoppingCartMapper shoppingCartMapper;
    @Autowired
    public OrderMapper orderMapper;
    @Autowired
    public UserServicelmpl userServicelmpl;
    @Autowired
    public AddressBookMapper addressBookMapper;
    @Override
    public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {
        AddressBook  addressBook=addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
        if(addressBook==null){
            throw new RuntimeException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }
        //查询购物车
        ShoppingCart shoppingCart=new ShoppingCart();
        List<ShoppingCart> list=orderMapper.findByUserId(userServicelmpl.getOpenid());
        //判断订单状态是否异常
        if(list==null || list.size()==0){
            throw new RuntimeException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
        //初始化Oder,把数据插入订单表
        Orders orders= new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO,orders);
        BeanUtils.copyProperties(ordersSubmitDTO, orders);
        orders.setOrderTime(LocalDateTime.now());
        orders.setPayStatus(Orders.UN_PAID);
        orders.setStatus(Orders.PENDING_PAYMENT);
        orders.setNumber(String.valueOf(System.currentTimeMillis()));
        orders.setAddress(addressBook.getDetail());
        orders.setPhone(addressBook.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setUserId(userServicelmpl.getOpenid());

        orderMapper.insert(orders);

        //把数据插入订单明细表
        for(int i=0;i<list.size();i++){
            ShoppingCart shoppingCart2=new ShoppingCart();
            OrderDetail orderDetail=new OrderDetail();
            shoppingCart2= list.get(i);
            BeanUtils.copyProperties(shoppingCart2,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderMapper.insertDetail(orderDetail);
        }

        //提交正常则清空购物车
        shoppingCartMapper.clean(userServicelmpl.openid);
        //返回OrderVO
        OrderSubmitVO orderSubmitVO=OrderSubmitVO.builder()
                .id(orders.getId())
                .orderTime(orders.getOrderTime())
                .orderNumber(orders.getNumber())
                .orderAmount(orders.getAmount())
                .build();

        return orderSubmitVO;
    }
}
